// $codepro.audit.disable lineLength, numericLiterals, methodChainLength
/**
 * Class: VIEW APPOINTMENT
 * Description:
 * 
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;

import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.LayoutStyle.ComponentPlacement;



/**
 * 
 * @author Team Awesome Expres
 * @version $ Revision 1.0 $
 *
 */
public class ViewAppointment {
	/**
	 * Field frmAppointmentPage.
	 */
	private static JFrame FrmAppointmentPage;

	/**
	 * Field myDataManager.
	 */
	private DataManager MyDataManager; // $codepro.audit.disable variableShouldBeFinal

	/**
	 * Field table.
	 */
	private JTable table;

	/**
	 * Field model.
	 */
	private ApptTableModel model;
	
	/**
	 * Field warning.
	 */
	private WarningMsg warning;
	
	/**
	 * Field makeNewAppt.
	 */
	private AppointmentFrame makeNewAppt;

	/**
	 * Table model class which automatically sets title and data, and updates table
	 * @author Marcus
	 */
    public static class ApptTableModel extends AbstractTableModel {
    	/**
    	 * Field data.
    	 */
    	public ArrayList<String[]> data = new ArrayList<String[]>();
		
        /**
         * Field title.
         */
        private final String[] title;

        /**
         * Field numOfAppt.
         */
        private final int numOfAppt;

        /**
         * Field colFrom.
         */
        private final int colFrom;

        /**
         * Field colTo.
         */
        private final int colTo;
        
        /**
         * Field newDataManager.
         */
        private final DataManager newDataManager;
        
        //Initially sets title and data for table
        /**
         * Constructor for ApptTableModel.
         * @param dataManager DataManager
         */
        public ApptTableModel(DataManager dataManager){
        	newDataManager = dataManager;
        	colFrom = newDataManager.getColumnPos(GlobalVars.APPOINTMENT_SHEET, "First Name");
        	colTo = newDataManager.getColumnPos(GlobalVars.APPOINTMENT_SHEET, "Reason for Appointment");
    		numOfAppt = newDataManager.getNumRows(GlobalVars.APPOINTMENT_SHEET);
    		
        	title = newDataManager.getDataAtRow(GlobalVars.APPOINTMENT_SHEET, colFrom, colTo, 1);
        	
        	for(int i=0; i < numOfAppt; i++){
				data.add(newDataManager.getDataAtRow(GlobalVars.APPOINTMENT_SHEET, colFrom, colTo, i + 2));
			}
        }

        /**
         * Method getColumnCount.
         * @return int
         * @see javax.swing.table.TableModel#getColumnCount()
         */
        @Override
        public int getColumnCount() {
            return title.length;
        }

        /**
         * Method getRowCount.
         * @return int
         * @see javax.swing.table.TableModel#getRowCount()
         */
        @Override
        public int getRowCount() {
            return data.size();
        }
             
		/**
		 * Method getValueAt.
		 * @param rowIndex int
		 * @param columnIndex int
		 * @return String
		 * @see javax.swing.table.TableModel#getValueAt(int, int)
		 */
		@Override
		public String getValueAt(int rowIndex, int columnIndex) {
			return data.get(rowIndex)[columnIndex];
		}
		
		/**
		 * Method getColumnName.
		 * @param column int
		 * @return String
		 * @see javax.swing.table.TableModel#getColumnName(int)
		 */
		@Override public String getColumnName(int column) {
			return title[column];
		}
		
		/**
		 * Method addData.
		 * @param newData String[]
		 */
		public void addData(String[] newData){
			final String[] apptData = new String[title.length];
			
			//table displays first name to reason for appointment(not username)
			for(int i=0; i < title.length; i++ ){
				apptData[i] = newData[colFrom + (i - 1)];
			}
			
			//add new appointment information
			newDataManager.setData(GlobalVars.APPOINTMENT_SHEET, null, newData, -1);
			FrmAppointmentPage.requestFocus();
			data.add(apptData);
			
			//updates the table
			//fireTableRowsInserted(first row, last row)
			fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
			//fireTableRowsUpdated(getRowCount()-1, getRowCount()-1);
			fireTableDataChanged();
		}
		
        /**
         * Method removeRow.
         * @param selectedRow int
         */
        public void removeRow(int selectedRow){
        	final String[] idList = newDataManager.getDataAtCol(GlobalVars.APPOINTMENT_SHEET, "Username*", "Username*");
        	newDataManager.deleteRow(GlobalVars.APPOINTMENT_SHEET, idList[selectedRow]);
        	data.remove(selectedRow);
        	fireTableDataChanged();
        }
       
      //only editable when "Edit" button pressed
        /**
         * Method isCellEditable.
         * @param row int
         * @param col int
         * @return boolean
         * @see javax.swing.table.TableModel#isCellEditable(int, int)
         */
        @Override public boolean isCellEditable(int row, int col) {
              return false;
          }
    }
     
	/**
	 * Create the application.
	 * @param dataManager DataManager
	 */
	public ViewAppointment(DataManager dataManager) {
		MyDataManager = dataManager;
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		FrmAppointmentPage = new JFrame();
		FrmAppointmentPage.getContentPane().setBackground(GlobalVars.BACKGROUND);
		FrmAppointmentPage.setTitle("Appointment Page");
		FrmAppointmentPage.setBounds(100, 100, 530, 420);
		FrmAppointmentPage.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		//get title and data for table
		model = new ApptTableModel(MyDataManager);
		final JScrollPane scrollPane = new JScrollPane();
		
		table = new JTable(model);
		scrollPane.setViewportView(table);
		
		final JButton btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				final int selectRow = table.getSelectedRow();
				if(selectRow < 0){
					warning = new WarningMsg();
				}
				else{
					model.removeRow(selectRow);
				}
			}
		});
		
		final JButton btnEdit = new JButton("Add");
		btnEdit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0){
				
				makeNewAppt = new AppointmentFrame(MyDataManager, MyDataManager.userType);
			}
		});
		
		final GroupLayout groupLayout = new GroupLayout(FrmAppointmentPage.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(169)
					.addComponent(btnEdit)
					.addGap(51)
					.addComponent(btnDelete)
					.addContainerGap(100, Short.MAX_VALUE))
				.addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 305, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnDelete)
						.addComponent(btnEdit))
					.addGap(19))
		);
		
		FrmAppointmentPage.getContentPane().setLayout(groupLayout);
		FrmAppointmentPage.setVisible(true);
	}

	/**
	 * Warning Message Pane for unselected deleting list
	 * @author Marcus
	 */
	public static class WarningMsg{
		/**
		 * Constructor for WarningMsg.
		 */
		public WarningMsg(){
			final String msg="Nothing has been selected.";
			JOptionPane.showMessageDialog(null, msg, "Warning", JOptionPane.WARNING_MESSAGE); //$codepro.audit.disable lineLength
		}
	}
}
